
//  MBMutiSearchBar.m
//  住房条件帅选器
//
//  Created by MB__Lee on 2018/5/15.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBMutiSearchBar.h"

#import "AreaTableview.h"  //区域
#import "PriceTableview.h" // 租金
#import "RoomTypeTableview.h" // 户型

#import "MBBTNTableviewFactory.h" // 更多

#import "BaseHousePostModel.h"



#define TopBarHeight  _OriginalHeight

@interface MBMutiSearchBar()<PriceTableviewDelegate,AreaTableviewDelegate,RoomTypeTableviewDelegate,MBBTNTableviewDelegate>

@property(nonatomic,strong)BaseHousePostModel *postModel;

@property(nonatomic,strong)NSMutableArray *btnMut; //顶部选项栏


@property(nonatomic,strong)AreaTableview     *ARTableview;
@property(nonatomic,strong)PriceTableview    *PRTableview;
@property(nonatomic,strong)RoomTypeTableview *RTTableview;
@property(nonatomic,strong)MBBTNTableview     *MRTableview;

@property(nonatomic,strong)UIButton *lastSelectedBTN;//上一次被选中的按钮

@property(nonatomic,assign)CGFloat OriginalHeight; //原来的高度
@property(nonatomic,strong)UIView *bottomView;//存放按钮的视图view

@end

@implementation MBMutiSearchBar

#pragma mark --懒加载
-(NSMutableArray *)btnMut{
    if (!_btnMut) {
        _btnMut = [NSMutableArray arrayWithObjects:@"区域",@"租金",@"户型",@"更多", nil];
    }
    return  _btnMut;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    //设置默认frame
//    CGRect realFrame =  CGRectMake(0, MBScale(20), self.bounds.size.width, MBScale(25));
    if (self  = [super initWithFrame: frame]) {
        
        //生成存放按钮的view
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        self.backgroundColor = [UIColor greenColor];
        //生成四个按钮
        [self MBMCreatFourButton];
        
        //生成上下分割线
        [self MBCreatUpDownLines];
        
        // 记录最初的高度
        _OriginalHeight = frame.size.height;
        
        //初始化网络请求数据model
        _postModel = [[BaseHousePostModel alloc]init];
    }
    return  self;
}
-(void)MBCreatUpDownLines{
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1)];
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
    line1.backgroundColor = MB_RGBA(242, 242, 242, 1.0);
    line2.backgroundColor = MB_RGBA(242, 242, 242, 1.0);
    [self addSubview:line1];
    [self addSubview:line2];
    
}
-(void)MBMCreatFourButton{
    
    CGFloat btnWidth = self.bounds.size.width * 0.25;
//    CGFloat btnHeight = MBScale(25);
    CGFloat btnHeight = self.bounds.size.height;
    
    for (int i = 0; i< 4; i++) {
        MSButton *button  = [MSButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(btnWidth * i, 0, btnWidth, btnHeight);
        [self addSubview:button];
        
        [button setTitle:self.btnMut[i] forState:UIControlStateNormal];
        [button setTitleColor:MB_mainColor forState:UIControlStateNormal];
        [button setTitleColor:MB_mainSelectedColor forState:UIControlStateSelected];
        button.titleLabel.font = MBFont(14);
        
        
        
        button.tag = 100 + i;
        [button addTarget:self action:@selector(MSButtonClik:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
}

#pragma mark --四个按钮的点击事件
-(void)MSButtonClik:(UIButton *)btn{
    NSInteger num = btn.tag - 100;
    //判断本次点击的按钮是不是上一次被选中的按钮
    if (btn.tag != _lastSelectedBTN.tag) {
        //移除按钮对应的视图移除掉
        [self removeSubviewWithTag:_lastSelectedBTN.tag - 100];
        _lastSelectedBTN.selected = NO;
    }
    _lastSelectedBTN = btn;
    btn.selected = !btn.selected;
    
    
    //根据按钮状态移除或者添加视图
    if (num == 0 ) {
        [self addOrRemoveWithBTN:btn view:self.ARTableview];
       
    }else if (num == 1){
        [self addOrRemoveWithBTN:btn view:self.PRTableview];
        
    }else if (num == 2){
        [self addOrRemoveWithBTN:btn view:self.RTTableview];
        
    }else if (num == 3){
        [self addOrRemoveWithBTN:btn view:self.MRTableview];
    }
    
    
}

  //按钮为选中状态显示大图，并且添加子视图；按钮为未选中状态的时候，移除子视图，仅显示搜索框概况
-(void)addOrRemoveWithBTN:(UIButton *)btn view:(UIView *)targetView{
     CGRect frame = self.frame;
    if (btn.selected) {
        [self addSubview:targetView];
    
        //根据不同的子视图来设置不同的高度
        frame.size.height = SCREEN_HIGHT - TopBarHeight;
        self.frame = frame;
    }else{
        [targetView removeFromSuperview];
        
        frame.size.height = _OriginalHeight;
        self.frame = frame;
    }

}

-(void)removeSubviewWithTag:(NSInteger)tag{
    if (tag == 0) {
        [self.ARTableview removeFromSuperview];
    }else if (tag == 1){
        [self.PRTableview removeFromSuperview];
    }else if (tag == 2){
        [self.RTTableview removeFromSuperview];
    }else if (tag == 3){
        [self.MRTableview removeFromSuperview];
    }
    
    
}


#pragma mark ---懒加载点击按钮召唤出来的视图
-(AreaTableview *)ARTableview{
    if (!_ARTableview) {
        _ARTableview = [[AreaTableview alloc] initWithFrame:CGRectMake(0, TopBarHeight, self.bounds.size.width, ArCellHeight * 7)];
        
        _ARTableview.backgroundColor = MB_RGBA(0.4, 0.2, 0.1, 1.0);
        _ARTableview.delegate = self;
        
    }
    return  _ARTableview;
}
-(PriceTableview *)PRTableview{
    if (!_PRTableview) {
        _PRTableview  = [[PriceTableview alloc] initWithFrame:CGRectMake(0, TopBarHeight, self.bounds.size.width, ArCellHeight *5) style:UITableViewStylePlain];
        _PRTableview.ptDelegate = self;
        
    }
    return  _PRTableview;
}
-(RoomTypeTableview *)RTTableview{
    if (!_RTTableview) {
        _RTTableview = [[RoomTypeTableview alloc] initWithFrame:CGRectMake(0, TopBarHeight, self.bounds.size.width, ArCellHeight *5) style:UITableViewStylePlain];
        _RTTableview.RTDelegate = self;
        
    }
    return  _RTTableview;
}
-(MBBTNTableview *)MRTableview{
    if (!_MRTableview) {
        CGFloat height = SCREEN_HIGHT - CGRectGetMaxY(self.frame) - 100;
        
//        _MRTableview = [[MBBTNTableview alloc] initWithFrame:CGRectMake(0, TopBarHeight, self.bounds.size.width, height) style:UITableViewStylePlain];
        
        _MRTableview  = [MBBTNTableviewFactory MBBTNTableviewFactoryCreatWithType:HouseTypeResidence withFrame:CGRectMake(0, TopBarHeight, self.bounds.size.width, height)];
        _MRTableview.BTNDelegate = self;
        
    }
    return  _MRTableview;
}
-(void)resetFrameAndRemoveView:(UIView *)targetView{
    
    [targetView removeFromSuperview];
    CGRect frame = self.frame;
    frame.size.height = _OriginalHeight;
    self.frame = frame;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self MSButtonClik:_lastSelectedBTN];
}

#pragma mark ---priceTableDelegate
-(void)priceTableview:(PriceTableview *)priceTableview priceModel:(PriceModel *)priceModel{
    

    _postModel.rent_start = priceModel.value_start;
    _postModel.rent_end = priceModel.value_end;
    
    //发送网络请求
    [self sendPostModelToDelegate];
    [self resetFrameAndRemoveView:priceTableview];
   
}
#pragma mark ----AreaTableview----区域代理，传递 区县、热门区域
-(void)AreaTableview:(AreaTableview *)areTable sendDistriceInfo:(NSString *)disctricName regionName:(NSString *)regionName{
    
}
-(void)AreaTableview:(AreaTableview *)areTable sendDistriceModel:(ArDistrictModel *)disctricModel regionModel:(ArRegionModel *)regionModel{
    
    //1、添加数据
    _postModel.city = disctricModel.code;
    _postModel.region = regionModel.code;
    //2、从父视图上移除
    [self resetFrameAndRemoveView:areTable];
}
#pragma mark --户型
-(void)RoomTypeTableview:(RoomTypeTableview *)roomTypeTableview sendRoomType:(NSString *)roomtype{
    //1、添加数据
    _postModel.bedroom = roomtype;
    //2、从父视图上移除
    [self resetFrameAndRemoveView:roomTypeTableview];
}
-(void)RoomTypeTableview:(RoomTypeTableview *)roomTypeTableview minSquare:(NSString *)minSquare maxSquare:(NSString *)maxSquare{
    
    
    //数据大小判断
    NSUInteger min = [minSquare integerValue];
    NSInteger max = [maxSquare integerValue];
    if (min > max) {
        minSquare = [NSString stringWithFormat:@"%zd",max];
        maxSquare = [NSString stringWithFormat:@"%zd",min];
    }
    
    //1、添加数据
    _postModel.square_start = minSquare;
    _postModel.square_end = maxSquare;
    //2、从父视图上移除
    [self resetFrameAndRemoveView:roomTypeTableview];
    
}

#pragma mark ---更多
-(void)MBBTNTableview:(MBBTNTableview *)tableview sendIndexPathArray:(NSMutableArray<NSIndexPath *> *)indexPathMut{
    
     //1、添加数据
    for (NSIndexPath *index in indexPathMut) {
        NSArray *array  = tableview.mut[index.section];
        NSString *name = array[index.row];
        NSLog(@"====%@",name);
    }
     //2、从父视图上移除
    [self resetFrameAndRemoveView:tableview];
    
}



-(void)sendPostModelToDelegate{
    
    //1、将模型发送给视图控制器
    
    //2、
    _lastSelectedBTN.selected = NO;
}

@end
